package main

import (
	"SearchEngine/index/getTmpIndexs"
	"SearchEngine/index/multipleMergeSort"
	"bufio"
	"fmt"
	"os"
	"strconv"
)

type tmpIndexs = getTmpIndexs.TmpIndex

func main() {
	//1.读取临时索引文件tmp_Index.bin，获取数据
	getTmpIndexs.GetTmpIndexs()
	//2.对临时索引进行排序
	tmpIndexs := multipleMergeSort.MergeSort(getTmpIndexs.TmpIndexs)
	//3.计算生成倒排索引,并存储（index.bin）
	//4.记录单词编号在索引文件中的偏移位置的文件（term_offset.bin）
	indexFilmName := "/home/fcou/go/src/SearchEngine/index/invertedIndex/index.bin"
	offsetFileName := "/home/fcou/go/src/SearchEngine/index/indexOffset/term_offset.bin"
	saveIndexTofile(indexFilmName, offsetFileName, tmpIndexs)
}

// 计算并保存倒排索引到文件（index.bin）
func saveIndexTofile(indexFilmName string, offsetFileName string, tmpIndexs []tmpIndexs) {
	//打开索引文件
	indexFile, err := os.OpenFile(indexFilmName, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0644)
	if err != nil {
		fmt.Println("saveIndexTofile:", err)
		return
	}
	defer indexFile.Close()
	indexWriter := bufio.NewWriter(indexFile)
	//打开索引偏移位置文件
	offsetFile, err := os.OpenFile(offsetFileName, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0644)
	if err != nil {
		fmt.Println("saveOffsetTofile:", err)
		return
	}
	defer offsetFile.Close()
	offsetWriter := bufio.NewWriter(offsetFile)
	//索引偏移位置
	var offsetNumber int64 = 0 //index第一行为空，从第二行开始算

	var preTmpID int64 = -1
	for _, tmpIndex := range tmpIndexs {
		//1.检查当前单词编号是否和前一个相同
		if preTmpID == tmpIndex.TermID {
			docIDString := strconv.Itoa(int(tmpIndex.DocID))
			indexWriter.WriteString(docIDString + ",")
			continue
		}
		//2.如果不同则，另起一行
		indexWriter.WriteString("\r\n")
		preTmpID = tmpIndex.TermID
		offsetNumber++ //偏移行数+1
		//offset文件先写入单词编号，再写入偏移位置
		termIDString := strconv.Itoa(int(tmpIndex.TermID))
		offsetWriter.WriteString(termIDString + "\t")
		offsetNumberString := strconv.Itoa(int(offsetNumber))
		offsetWriter.WriteString(offsetNumberString + "\r\n")
		//index文件先写入单词编号，再写入网页编号
		indexWriter.WriteString(termIDString + "\t")
		docIDString := strconv.Itoa(int(tmpIndex.DocID))
		indexWriter.WriteString(docIDString + ",")
	}
	offsetWriter.Flush()
	indexWriter.Flush()
}
